package com.ywgt.lock8;

import java.util.concurrent.TimeUnit;

public class Test1 {
    public static void main(String[] args) {
        Phone phone=new Phone();

        new Thread(()->{
            phone.sendMsg();
        }).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            phone.call();
        }).start();

    }
}
class Phone{
    //被synchronized锁的对象就是phone所以谁先拿到谁先执行
    public synchronized void call(){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"打电话");
    }
    public synchronized void sendMsg(){
        System.out.println(Thread.currentThread().getName()+"发短信");
    }
}